import 'package:flutter/material.dart';
import 'dart:async';
import 'package:scoped_model/scoped_model.dart';
import '../../ScopedModels/ScopedMain.dart';
class DetailPage extends StatelessWidget {
 
final int productIndex;
DetailPage(this.productIndex);
  @override
  Widget build(BuildContext context) {
    print('[Products Widget] build');
    // TODO: implement build
    return ScopedModelDescendant(builder: (BuildContext context, Widget child, ScopedMain model){
      return  WillPopScope(
      onWillPop: () {
        print('点了返回');
        model.selectProductIndex(null);
        Navigator.pop(context, false);

        return Future.value(false);
      },
      child: Scaffold(
          appBar: AppBar(
            title: Text(model.products[productIndex].title),
          ),
          body: Column(
            children: <Widget>[
              Container(
                child: Image.asset(model.products[productIndex].imageURL),
                padding: EdgeInsets.all(10.0),
              ),
              RaisedButton(
                  child: Text('返回'),
                  onPressed: () => Navigator.pop(context, false)),
              Container(
                child: RaisedButton(
                    child: Text('删除'),
                    //=> 这里要用箭头函数
                    onPressed: () => _showWarningAlert(context)),
                padding: EdgeInsets.all(10.0),
              )
            ],
          )),
    );
    });
    //监听导航条返回按钮
    
  }

  _showWarningAlert(BuildContext context) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('确定删除吗'),
            content: Text('无法恢复'),
            actions: <Widget>[
              FlatButton(
                child: Text('取消'),
                onPressed: () {
                  Navigator.pop(context);
                },
              ),
              FlatButton(
                child: Text('删除'),
                onPressed: () {
                  Navigator.pop(context);
                  Navigator.pop(context, true);
                },
              )
            ],
          );
        });
  }
}
